﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Util;
using DotNet.Common.Command;
using ZLCMS.Data.Model;
using NHibernate;
using System.Collections;
using ZLCMS.Commands.User.Commands.Reconciliations;
using ZLCMS.Commands.User.Util;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Reconciliations
{
    public partial class BillWeeklyDetail : System.Web.UI.Page
    {
        protected FrameHelper _framePage;
        protected string _userAccount;
        string billNo;

        protected bool ChangeStatusRight{get;set;}

        protected bool AllRight { get; set; }

        protected ShopBillWeekly Data { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            AllRight = AuthUtil.CheckAuthorization("BILL_MAN", "VIEW_ALL", _userAccount);
            ChangeStatusRight = AllRight || AuthUtil.CheckAuthorization("BILL_MAN", "CHANGE_STATUS", _userAccount);
          
            if (!IsPostBack)
            {
                billNo = Request["bno"] == null ? "" : Request["bno"];
                _billNo.Text = billNo;
                _deptCode.Text = Request["dc"] == null ? "" : Request["dc"];
            }
            else
            {
                billNo = _billNo.Text;
                Save();
            }

            LoadBillDetailResult result = new LoadBillDetailCommand().Execute(new LoadBillDetailParameter()
            {
                BillNo = billNo,
                DeptCode = _deptCode.Text
            }).Result;

            Data = result.Data;
            if (Data != null)
            {
                status.SelectedValue = Data.Status;
                tb_fillFee.Text = string.Format("{0:0.00}", Data.FillFee);
                if (Data.Status=="9")
                {
                    status.Enabled = false;
                }
            }
        }

        protected void Save()
        {
            string stat = status.SelectedValue;
            if (!string.IsNullOrEmpty(stat) && ChangeStatusRight)
            {
                decimal fillFee = Convert.ToDecimal(tb_fillFee.Text);
                if (!ChangeStatusRight)
                {
                    fillFee = 0;
                }
                ICommandResult<UpdateBillStatusResult> res = new UpdateBillStatusCommand().Execute(new UpdateBillStatusParameter()
                {
                    BillNo = _billNo.Text,
                    DeptCode = _deptCode.Text,
                    FillFee=fillFee,
                    Status = stat,
                    UserAccount = _userAccount
                });
                if (res.Error)
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 300,
                        message = "操作失败!",
                        callbackType = "forward",
                        forwardUrl = "/SystemManage/Basics/Shop/Reconciliations/BillDetail.aspx?tabid=BillDetail&bno=" + _billNo.Text + "&dc=" + _deptCode.Text
                    });
                }
                else
                {
                    _framePage.Alert(new ReturnCode()
                    {
                        statusCode = 200,
                        message = "操作成功!",
                        callbackType = "forward",
                        forwardUrl = "/SystemManage/Basics/Shop/Reconciliations/BillDetail.aspx?tabid=BillDetail&bno=" + _billNo.Text + "&dc=" + _deptCode.Text
                    });
                }
            }

        }
    }
}
